package com.example.nbb.Service;

import com.example.nbb.Dao.FootPrintsDao;
import com.example.nbb.Entity.FootPrintsEntity;
import com.example.nbb.Entity.ResultEntity;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class FootPrintsService {
    @Resource
    FootPrintsDao footPrintsDao;

    private static final Logger logger = LoggerFactory.getLogger(FootPrintsService.class);//日志。getLogger需要传入当前的类名

    /**
     * 查询用户所有足迹
     *
     * @param userId 用户id
     * @return 返回
     */
    public String selectAll(int userId) {
        try {
            return ResultEntity.success(footPrintsDao.selectAll(userId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 添加足迹
     *
     * @param userId    用户id
     * @param productId 商品id
     * @return 返回
     */
    public String addFootPrints(int userId, int productId) {
        try {
            //先传入用户id和当前页面的商品id 判断当前足迹表是不是有数据
            FootPrintsEntity footPrintsEntity = footPrintsDao.selectDetail(userId, productId);
            if (footPrintsEntity == null) {
                //表示当前没数据 那么就直接插入数据
                return ResultEntity.success(footPrintsDao.addFootPrints(userId, productId));
            } else {
                //表示当前有数据  那么更新时间
                return ResultEntity.success(footPrintsDao.updateTime(userId, productId));
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }


}
